#!/usr/bin/perl -w

#=======================================================================
# ep-pause
# File ID: 1f845a80-5d39-11df-95dd-90e6ba3022ac
# Finner pauser mellom datoer på epoch-format.
#
# Character set: UTF-8
# ©opyleft 2004– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License, see end of file for legal stuff.
#=======================================================================

use strict;
use Time::Local;

$| = 1;

use Getopt::Std;
our ($opt_h, $opt_l, $opt_s) =
    (     0,      1,      0);
getopts('hl:s') || die("Option error. Use -h for help.\n");

my $VERSION = "0.0";

our $progname = $0;
$progname =~ s#^.*/(.*?)$#$1#;

$opt_h && usage(0);

my $last_sec = 0;
my $d_sep = "-";
my $dt_sep = "T";
my $t_sep = ":";

while (<>) {
    if (/^.*?(\d+).*?$/) {
        my $Secs = $1;
        my $Pause = $Secs - $last_sec;
        if ($last_sec && ($Pause >= $opt_l)) {
            if ($opt_s) {
                my @TA = gmtime($last_sec);
                printf("%04u$d_sep%02u$d_sep%02u$dt_sep%02u$t_sep%02u$t_sep%02uZ\t$Pause\n",
                $TA[5]+1900, $TA[4]+1, $TA[3], $TA[2], $TA[1], $TA[0]);
            } else {
                printf(
                    "%u-%u\t%u\t%s\n",
                    $last_sec, $Secs,
                    $Pause, sec_to_hms($Pause)
                );
            }
        }
        $last_sec = $Secs;
    } else {
        print(STDERR "Ukjent linje $.\n");
    }
}

sub sec_to_hms {
	# {{{
	my $secs = shift;
	my ($Day, $Hour, $Min, $Sec) = (0, 0, 0, 0);

	$Day = int($secs/86400);
	$secs -= $Day*86400;

	$Hour = int($secs/3600);
	$secs -= $Hour * 3600;

	$Min = int($secs/60);
	$secs -= $Min * 60;

	$Sec = $secs;

	return(($Day ? "$Day:" : "") . sprintf("%02u:%02u:%02u", $Hour, $Min, $Sec));
	# }}}
} # Tidsperiode()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;
    print(<<END);
$progname v$VERSION

Usage: $progname [options] [file [...]]

Scans Apache access logs and prints out time periods with no connections.

Options:

-h    Print this help.
-l x  Show only pauses longer than x seconds.

END
    exit($Retval);
    # }}}
}

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME

ep-pause — find pauses in Apache logs

=head1 SYNOPSIS

ep-pause [options] [file [...]]

=head1 DESCRIPTION



=head1 OPTIONS

=over 4

=item B<-h>

Print a brief help summary.

=item B<-l> x

Only list pauses longer than I<x> seconds.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny _AT_ sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm &lt;sunny@sunbase.org&gt;
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation; either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program; if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
# End of file ep-pause
